﻿using Butcher.winform.Dto;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Butcher.winform.EnterpriseForms
{
    public partial class ApproachView : Form
    {
        public ApproachView()
        {
            InitializeComponent();
            var NowDate = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day;
            var a = 1;
            tb_ApproachNo.Text = NowDate + a;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ApproachCreateDTO approach = new ApproachCreateDTO();
            approach.ApproachTime = Convert.ToDateTime(dt_ApproachTime.Text);
            approach.licensePlate = tb_licensePlate.Text;

            if (rb_SettlementType1.Checked)
            {
                approach.SettlementType = rb_SettlementType1.Text;

            }
            else if (rb_SettlementType2.Checked)
            {
                approach.SettlementType = rb_SettlementType2.Text;
                tb_Suppliers.Text = "供应商";
            }
            else if (rb_SettlementType3.Checked)
            {
                approach.SettlementType = rb_SettlementType3.Text;
                tb_Suppliers.Text = "供应商";
            }
            else
            {
                approach.SettlementType = rb_SettlementType4.Text;
                tb_Suppliers.Text = "客户";
            }
            approach.ApproachNo = tb_ApproachNo.Text;
            approach.Suppliers = tb_Suppliers.Text;
            approach.ApproachBatch = tb_ApproachBatch.Text;
            approach.Approachnum = Convert.ToInt32(tb_ApproachNun.Text);
            approach.QuarantineCertificateNo = tb_QuarantimeCertificateNo.Text;
            approach.Address = tb_Address.Text;
            approach.Contact = tb_Contact.Text;
            approach.ContactPhone = tb_Phone.Text;
            approach.IdentificationCard = tb_IdentificationNumber.Text;
            approach.Remark = tb_Remark.Text;
            approach.QuarantineCertificateImg = Convert.ToString(pb_QuarantineCertificateImg.Image);
            approach.GoOutTime = Convert.ToDateTime(dt_ApproachTime.Text).AddMinutes(3);
            approach.InUserId = 1;
            approach.GoUserId = 1;
            approach.ShoppingDesignationid = "生猪";
            HttpClient client = new HttpClient();
            string urlSelect = @"http://localhost:5229/api/Enterprise/SelectApproachBatch?Batch=" + tb_ApproachBatch.Text;
            string res = client.GetStringAsync(urlSelect).Result;
            JObject infos = JObject.Parse(res);
            int resCode = infos["code"].Value<int>();
            if (resCode > 0)
            {
                if (MessageBox.Show("今天已经存在此进场批次，是否将两个批次进行合并?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    approach.Pid = 1;
                    this.Hide();
                }
                else
                {
                    approach.Pid = 0;
                    this.Hide();
                }

            }
            string url = @"http://localhost:5229/api/Enterprise/CreateApproacgh";

            var content = new StringContent(JsonSerializer.Serialize<ApproachCreateDTO>(approach));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = "";
            saaa = info["messges"].Value<string>();
            if (saaa != null)
            {
                MessageBox.Show("新增成功");
            }
            else
            {
                MessageBox.Show("新增失败");
            }
        }

        private void rb_SettlementType1_CheckedChanged(object sender, EventArgs e)
        {
            tb_Suppliers.Text = "供应商";
        }

        private void rb_SettlementType2_CheckedChanged(object sender, EventArgs e)
        {
            tb_Suppliers.Text = "供应商";
        }

        private void rb_SettlementType3_CheckedChanged(object sender, EventArgs e)
        {
            tb_Suppliers.Text = "供应商";
        }

        private void rb_SettlementType4_CheckedChanged(object sender, EventArgs e)
        {
            tb_Suppliers.Text = "客户";
        }
    }
}
